package booklib.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import booklib.dao.UserDao;
import booklib.model.User;
import booklib.util.DbUtil;
import booklib.util.StringUtil;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LogInFrame extends JFrame {

	private JPanel contentPane;
	private JTextField userNametxt;
	private JPasswordField passwordtxt;
	
	private DbUtil dbu=new DbUtil();
	private UserDao userDao=new UserDao();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogInFrame frame = new LogInFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LogInFrame() {
		setResizable(false);
		setTitle("管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 612, 388);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("图书管理系统");
		lblNewLabel.setFont(new Font("华文行楷", Font.BOLD | Font.ITALIC, 30));
		lblNewLabel.setIcon(new ImageIcon(LogInFrame.class.getResource("/images/book_61.44px_1146884_easyicon.net.png")));
		
		JLabel lblNewLabel_1 = new JLabel("用户名");
		lblNewLabel_1.setIcon(new ImageIcon(LogInFrame.class.getResource("/images/add_user_21.710391822828px_1253559_easyicon.net.png")));
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 18));
		
		JLabel lblNewLabel_2 = new JLabel("密 码");
		lblNewLabel_2.setIcon(new ImageIcon(LogInFrame.class.getResource("/images/password_23.969348659004px_1230191_easyicon.net.png")));
		lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 18));
		
		userNametxt = new JTextField();
		userNametxt.setColumns(10);
		
		passwordtxt = new JPasswordField();
		
		JButton btnNewButton = new JButton("登录");
		
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logInActionPerform(e);
			}
		});
		
		btnNewButton.setFont(new Font("宋体", Font.BOLD, 18));
		btnNewButton.setIcon(new ImageIcon(LogInFrame.class.getResource("/images/login_24px_505036_easyicon.net.png")));
		
		
		JButton btnNewButton_1 = new JButton("注册");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegisterActionPerform(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(LogInFrame.class.getResource("/images/reset_24px_28025_easyicon.net.png")));
		btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 18));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(153, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addGap(79)
					.addComponent(btnNewButton_1)
					.addGap(166))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 283, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(125)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_1))
							.addGap(40)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(passwordtxt, GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
								.addComponent(userNametxt, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))))
					.addContainerGap(137, GroupLayout.PREFERRED_SIZE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(userNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(35)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(passwordtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addGap(34)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton))
					.addGap(25))
		);
		contentPane.setLayout(gl_contentPane);
		this.setLocationRelativeTo(null);
	}
	/**
	 * 登录事件
	 * @param e
	 */
	private void logInActionPerform(ActionEvent e) {
		// TODO 自动生成的方法存根
		String userName=this.userNametxt.getText();
		String password=new String(this.passwordtxt.getPassword());
		if(StringUtil.isEmpty(userName)){
			JOptionPane.showMessageDialog(null, "用户名不能为空");
		}
		if(StringUtil.isEmpty(password)){
			JOptionPane.showMessageDialog(null, "密码不能为空");
		}
		User user=new User(userName,password);
		Connection con=null;
		try {
			con=dbu.getCon();
			User currentUser=userDao.login(con, user);
			if(currentUser!=null){
				dispose();
				new MainFrame().setVisible(true);
			}
			else{
				JOptionPane.showMessageDialog(null,"用户名或密码错误");
			}
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}finally{
			try {
				dbu.closeCon(con);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 注册事件
	 * @param e
	 */
	private void RegisterActionPerform(ActionEvent e) {
		// TODO 自动生成的方法存根
		String userName=this.userNametxt.getText();
		String password=new String(this.passwordtxt.getPassword());
		if(StringUtil.isEmpty(userName)){
			JOptionPane.showMessageDialog(null, "用户名不能为空");
		}
		if(StringUtil.isEmpty(password)){
			JOptionPane.showMessageDialog(null, "密码不能为空");
		}
		User user=new User(userName,password);
		Connection con=null;
		try {
			con=dbu.getCon();
			int addNum=userDao.Register(con, user);
			if(addNum==1){
				JOptionPane.showMessageDialog(null,"注册成功");
			}
			else {
				JOptionPane.showMessageDialog(null,"注册失败，用户名已存在");
			}
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null,"注册失败");
		}finally{
			try {
				dbu.closeCon(con);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 重置事件处理
	 * @param e
	 */
	private void resetValueActionPerform(ActionEvent e) {
		// TODO 自动生成的方法存根
		this.userNametxt.setText("");
		this.passwordtxt.setText("");
	}
}
